<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod__product">
      <el-form :inline="true" :model="query" @keyup.enter.native="getData()">
  <el-form-item>
    <el-input v-model="query.carNumber" placeholder="车牌号" clearable></el-input>
  </el-form-item>
  <el-form-item>
    <el-select v-model="query.isBanner" placeholder="请选择是否推荐">
                    <el-option key="1" label="不推荐" value="1"></el-option>
                    <el-option key="2" label="推荐" value="2"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-select v-model="query.status" placeholder="请选择状态">
                    <el-option key="1" label="空闲中" value="1"></el-option>
                    <el-option key="2" label="已租赁" value="2"></el-option>
    </el-select>
  </el-form-item>
    <el-form-item >
        <el-input v-model="query.brandName" placeholder="品牌名" clearable></el-input>
    </el-form-item>
            <el-form-item >
        <el-input v-model="query.categoryName" placeholder="类型" clearable></el-input>
    </el-form-item>
            <el-form-item >
        <el-input v-model="query.sortName" placeholder="投放点" clearable></el-input>
    </el-form-item>
           <el-form-item>
          <el-button @click="getData()" type="success">查询</el-button>
          <el-button @click="reset()">重置</el-button>
   </el-form-item>
            <el-form-item>
            <el-button type="primary"  @click="addProductInfo">添加</el-button>
        </el-form-item>
                <el-form-item>
            <el-button :disabled="this.multipleSelection.length === 0" type="danger"  @click="delBatchProductInfo">批量删除</el-button>
        </el-form-item>
            <el-form-item>
        <el-button type="primary" @click="createProductInfo">生成数据</el-button>
    </el-form-item>

  </el-form>
      <el-table v-loading="tableDataLoading" :data="tableData" border @selection-change="selectionChange" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="brandName" label="品牌名" header-align="center" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.brandName !=null">{{scope.row.brandName}}</span>
              <span v-else>暂无</span>
          </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="类型" header-align="center" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.categoryName !=null">{{scope.row.categoryName}}</span>
              <span v-else>暂无</span>
          </template>
      </el-table-column>
      <el-table-column prop="sortName" label="投放点" header-align="center" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.sortName !=null">{{scope.row.sortName}}</span>
              <span v-else>暂无</span>
          </template>
      </el-table-column>
     <el-table-column prop="carNumber" label="车牌号" header-align="center" align="center">
         <template slot-scope="scope">
         <span v-if="scope.row.carNumber !=null">{{scope.row.carNumber}}</span>
         <span v-else>暂无</span>
         </template>
     </el-table-column>
     <el-table-column prop="color" label="颜色" header-align="center" align="center">
         <template slot-scope="scope">
         <span v-if="scope.row.color !=null">{{scope.row.color}}</span>
         <span v-else>暂无</span>
         </template>
     </el-table-column>
     <el-table-column prop="price" label="价格/h" header-align="center" align="center">
         <template slot-scope="scope">
         <span v-if="scope.row.price !=null">{{scope.row.price}}</span>
         <span v-else>暂无</span>
         </template>
     </el-table-column>
       <el-table-column prop="imageUrl" label="图片"  header-align="center" align="center">
          <template slot-scope="scope">
              <el-image
                      style="width: 60px; height: 60px"
                      v-if="scope.row.imageUrl != null"
                      :src="scope.row.imageUrl"
                      :preview-src-list="[scope.row.imageUrl]">
              </el-image>
              <span v-else>暂无图片</span>
          </template>
       </el-table-column>
       <el-table-column prop="isBanner" label="是否推荐"  header-align="center" align="center">
         <template slot-scope="scope">
             <el-tag type="success" v-if="scope.row.isBanner === 1">不推荐</el-tag>
             <el-tag type="success" v-if="scope.row.isBanner === 2">推荐</el-tag>
         </template>
       </el-table-column>
       <el-table-column prop="status" label="状态"  header-align="center" align="center">
         <template slot-scope="scope">
             <el-tag type="success" v-if="scope.row.status === 1">空闲中</el-tag>
             <el-tag type="success" v-if="scope.row.status === 2">已租赁</el-tag>
         </template>
       </el-table-column>
       <el-table-column prop="createTime" label="创建时间" width="160" header-align="center" align="center"></el-table-column>
       <el-table-column prop="updateTime" label="更新时间" width="160" header-align="center" align="center"></el-table-column>
            <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150" >
            <template slot-scope="scope">
                    <el-button type="success"  size="small"  @click="updateProductInfo(scope.row)">更新</el-button>
                    <el-button type="danger"  size="small"  @click="delProductInfo(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
     </el-table>
      <el-pagination
              :current-page="query.pageNum"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="query.pageSize"
              :total="total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="pageSizeChange"
              @current-change="pageCurrentChange">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
 <el-dialog  :visible.sync="addOrUpdateVisible" :title="!form.id ? '新增' : '编辑'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="form" :rules="rule" ref="form" label-width="100px">
    <el-form-item label="车牌号" prop="carNumber">
        <el-input v-model="form.carNumber" placeholder="车牌号"></el-input>
    </el-form-item>
    <el-form-item label="颜色" prop="color">
        <el-input v-model="form.color" placeholder="颜色"></el-input>
    </el-form-item>
    <el-form-item label="价格/h" prop="price">
        <el-input v-model="form.price" placeholder="价格/h"></el-input>
    </el-form-item>
      <el-form-item label="图片" prop="imageUrl">
          <single-upload v-model="form.imageUrl"></single-upload>
      </el-form-item>
      <el-form-item label="是否推荐" prop="isBanner">
          <el-radio-group v-model="form.isBanner">
    <el-radio :label="1">不推荐</el-radio>
    <el-radio :label="2">推荐</el-radio>
          </el-radio-group>
          </el-form-item>
      <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
    <el-radio :label="1">空闲中</el-radio>
    <el-radio :label="2">已租赁</el-radio>
          </el-radio-group>
          </el-form-item> -->
    <el-form-item label="品牌" prop="brandId">
        <el-select v-model="form.brandId" placeholder="请选择品牌">
            <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.brandName"
                    :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="类型" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择类型">
            <el-option
                    v-for="item in options2"
                    :key="item.id"
                    :label="item.categoryName"
                    :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="投放点" prop="sortId">
        <el-select v-model="form.sortId" placeholder="请选择投放点">
            <el-option
                    v-for="item in options3"
                    :key="item.id"
                    :label="item.sortName"
                    :value="item.id">
            </el-option>
        </el-select>
    </el-form-item>
 </el-form>
        <template slot="footer">
          <el-button @click="addOrUpdateVisible = false">取消</el-button>
          <el-button type="primary" @click="formSubmitHandle()">确认</el-button>
        </template>
</el-dialog>


    </div>
  </el-card>
</template>

<script>
    import { } from '@/utils/validator'

    import {getProductList,saveProductInfo,updateProductInfo,delProductInfo,delBatchProductInfo,createProductData,} from '@/api/product'
                                import {getBrandList,} from '@/api/brand'
                                            import {getCategoryList,} from '@/api/category'
                                            import {getSortList,} from '@/api/sort'
                    import SingleUpload from '@/components/common/singleUpload.vue';


  export default {
    components: {
    SingleUpload,
    },

    data () {
      return {
        options:[],
        options2:[],
        options3:[],
        tableData: [],
        query: {
          pageNum: 1,
          pageSize: 10,

          carNumber:null,
          brandId:null,
          categoryId:null,
          sortId:null,
          isBanner:null,
          status:null,
          brandName:null,
          categoryName:null,
          sortName:null,
        },
        form: {},
        multipleSelection: [],
        tableDataLoading: false,
        total: 0,
        addOrUpdateVisible: false,
        rule: {
         carNumber: [{required: true, message: '车牌号不得为空', trigger: "blur"}],
         color: [{required: true, message: '颜色不得为空', trigger: "blur"}],
         price: [{required: true, message: '价格/h不得为空', trigger: "blur"}],
        },
    }
   },
    created() {
      this.getData();
    },
    computed:{
        userType(){
            return Number(sessionStorage.getItem('userType'))
        }
    },

    methods:{


      /**
       * 获取所有车辆
       */
      getData(){
        getProductList(this.query).then(res =>{
          this.tableData = res.data.list;
          this.total = res.data.total;
        });
            getBrandList({pageSize:100}).then(res =>{
            this.options = res.data.list;
        });

            getCategoryList({pageSize:100}).then(res =>{
            this.options2 = res.data.list;
        });

            getSortList({pageSize:100}).then(res =>{
            this.options3 = res.data.list;
        });

      },

      /**
       * 打开添加框
       */
      addProductInfo(){
        this.form = {};
        this.form.id = null;
        if (this.$refs['form'] !==undefined){
          this.$refs['form'].resetFields();
        }
        this.addOrUpdateVisible = true;
      },

      /**
       * 打开更新框
       */
      updateProductInfo(row){
        this.form = JSON.parse(JSON.stringify(row));
        this.addOrUpdateVisible = true;
        },

      /**
       * 按住enter键或者确定按钮提交数据,对后台发请求
       * 如果id为null,就是走添加车辆的接口
       * 如果id不为null,就是走更新车辆的接口
       */
      formSubmitHandle(){
        this.$refs['form'].validate(valid => {
                if(valid){
          if (this.form.id == null){
        saveProductInfo(this.form).then(res =>{
            if (res.code === 1){
                this.$message.success('添加成功');
                this.getData();
                this.addOrUpdateVisible = false;
            }
            })
          }else {
        updateProductInfo(this.form).then(res =>{
              this.$message.success('更新成功');
              this.getData();
              this.addOrUpdateVisible = false;
            })
          }
          // this.$refs['form'].resetFields()

        }
        })
      },

      /**
       *单个删除数据
       */
      delProductInfo(id){
        this.$confirm('确定要删除所选择的吗？', '提示', {
          type: 'warning'
        }).then(action => {
          if (action === 'confirm') {
            delProductInfo(id).then(res=>{
              if (res.code === 1){
                this.$message.success('删除成功');
                this.getData();
              }else {
                return this.$message.error(res.msg)
              }

            })
          }
        }).catch(() => {});

      },



      /**
       * 选中table表格事件
       */
      selectionChange(val){
        this.multipleSelection = [];
        val.forEach((item) => {
          this.multipleSelection.push(item.id);
        });
      },

        /**
         * 批量删除车辆的接口
         */
      delBatchProductInfo(){
        this.$confirm('确定要删除所选择的吗？', '提示', {
            type: 'warning'
        }).then(action => {
            if (action === 'confirm') {
                delBatchProductInfo(this.multipleSelection.join(',')).then(res =>{
                    this.$message.success('批量删除成功');
                    this.getData();
                })
            }
        }).catch(() => {});

      },

      /**
       * 重置数据
       */
      reset(){
        this.query.carNumber = null;
        this.query.brandId = null;
        this.query.categoryId = null;
        this.query.sortId = null;
        this.query.isBanner = null;
        this.query.status = null;
             this.query.brandName=null
                  this.query.categoryName=null
                  this.query.sortName=null
             this.$set(this.query, 'pageNum', 1);
        this.getData();
      },



      /**
       * 分页(改变页码)
       * @param val
       */
      pageCurrentChange(val){
        this.$set(this.query, 'pageNum', val);
        this.getData();
      },

      /**
       * 分页(改变每页条数)
       * @param val
       */
      pageSizeChange(val){
        this.$set(this.query, 'pageSize', val);
        this.getData();
      },

      /**
       * 关闭弹窗
       */
     clearAddForm(){
     this.addOrUpdateVisible = false;
    },


    createProductInfo(){
        createProductData().then(res =>{
            this.getData();
            this.$message.success('操作成功')
        })
    }

    },


  }
</script>


<style scoped>

                                                                                                    </style>
